﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Moonlite.ExtensionApi.XimlInterfaces;
using Moonlite.Platform.General;

namespace Moonlite.Platform.Ximl
{
    public class BarItemObject : IBarItemObject
    {
        private ImageList imageList;
        private int currentImageIndex;
        private DescriptorBase descriptor;
        public Type DesiredDescriptorType
        {
            get { return InterfaceTypes.BarItemDescriptorType; }
        }
        public DescriptorBase Descriptor
        {
            get { return descriptor; }
        }
        private void AddBarItem(BarItemDescriptor barItemDescriptor, BarLinkContainerItem barItem)
        {
            BarItem button = null;
            if (barItemDescriptor.Children.Count > 0)
            {
                button = new BarLinkContainerItem();
                button.Caption = barItemDescriptor.Text;
            }
            else
            {
                button = new BarButtonItem(((BarManager)((object[])(Global.Instance.GetService(typeof(BarManager))))[0]), barItemDescriptor.Text, -1);
            }
            if (button != null && button is BarLinkContainerItem)
            {
                foreach (DescriptorBase childDescriptor in barItemDescriptor.Children)
                {
                    if (childDescriptor is BarItemDescriptor)
                    {
                        AddBarItem((BarItemDescriptor)childDescriptor, (BarLinkContainerItem)button);
                    }
                }
            }
            if (button != null)
            {
                button.Tag = barItemDescriptor;
                button.ItemClick += XimlEventHandler.OnButtonClicked;
                button.Glyph = barItemDescriptor.Icon;
                BarItemLink link = null;
                for (int i = 0; i < barItem.Links.Count; i++)
                {
                    if (i == barItemDescriptor.Index)
                    {
                        link = barItem.Links[i];
                        break;
                    }
                }
                if (link == null)
                {
                    barItem.AddItem(button).BeginGroup = barItemDescriptor.Separator;
                }
                else
                {
                    barItem.InsertItem(link, button).BeginGroup = barItemDescriptor.Separator;
                }
            }
        }
        public void Create(DescriptorBase descriptor)
        {
            if (Global.Instance == null || Global.Instance.GetService(typeof(BarManager)) == null || descriptor == null) return;
            currentImageIndex = 0;
            imageList = new ImageList();
            BarItemDescriptor barItemDescriptor = (BarItemDescriptor)descriptor;
            this.descriptor = descriptor;
            if (XimlTree.Instance.IsNativeElement(barItemDescriptor.Guid.ToString()))
            {
                BarLinkContainerItem barItem = (BarLinkContainerItem)barItemDescriptor.Tag;
                if (barItem != null)
                {
                    foreach (DescriptorBase child in barItemDescriptor.Children)
                    {
                        if (child is BarItemDescriptor)
                        {
                            BarItemDescriptor childBarItemDescriptor = (BarItemDescriptor)child;
                            if (childBarItemDescriptor.Icon != null)
                            {
                                imageList.Images.Add(childBarItemDescriptor.Icon);
                            }
                            AddBarItem(childBarItemDescriptor, barItem);
                            if (childBarItemDescriptor.Icon != null)
                            {
                                currentImageIndex++;
                            }
                        }
                    }
                }
            }
        }
    }
}
